package org.asiainfo.file.service.impl;

import org.asiainfo.common.core.utils.StringUtils;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.file.domain.table.AttactmentTableDef;
import org.asiainfo.file.domain.vo.AttactmentVo;
import org.asiainfo.file.domain.bo.AttactmentBo;
import org.asiainfo.file.domain.Attactment;
import org.asiainfo.file.service.AttactmentService;
import org.springframework.stereotype.Service;
import org.asiainfo.common.core.utils.MapstructUtils;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.asiainfo.file.mapper.AttactmentMapper;
import com.mybatisflex.core.paginate.Page;

import java.util.Map;
import java.util.Collection;
import java.util.List;

import static com.mybatisflex.core.query.QueryMethods.bracket;

/**
 * 附件文件信息(Attactment)表服务实现类
 *
 * @author dotor-ww
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AttactmentServiceImpl extends ServiceImpl<AttactmentMapper, Attactment> implements AttactmentService {

    /**
     * 查询
     *
     * @param attactmentBo 筛选条件
     * @return 查询结果
     */
    @Override
    public List<AttactmentVo> queryList(AttactmentBo attactmentBo) {
        Map<String, Object> params = attactmentBo.getParams();
        QueryWrapper query = query();
        return listAs(query, AttactmentVo.class);
    }

    /**
     * 通过attactId查询单条数据
     *
     * @param attactId 主键
     * @return 实例对象
     */
    @Override
    public AttactmentVo queryById(Long attactId) {
        return getOneAs(query().eq(Attactment::getAttactId, attactId), AttactmentVo.class);
    }

    /**
     * 分页查询
     *
     * @param attactmentBo 筛选条件
     * @param pageQuery    分页对象
     * @return 查询结果
     */
    @Override
    public TableDataInfo<AttactmentVo> queryPageList(AttactmentBo attactmentBo, PageQuery pageQuery) {
        Map<String, Object> params = attactmentBo.getParams();
        QueryWrapper query = query().between(Attactment::getCreateTime, params.get("beginTime"), params.get("endTime"),
                ObjectUtil.isNotNull(params.get("beginTime")) && ObjectUtil.isNotNull(params.get("endTime")));
        pageQuery.buildOrder(query);
        query.like(Attactment::getApplyName, attactmentBo.getApplyName(), StringUtils.isNotBlank(attactmentBo.getApplyName()));
        query.like(Attactment::getApplyCode, attactmentBo.getApplyCode(), StringUtils.isNotBlank(attactmentBo.getApplyCode()));
        Page<AttactmentVo> page = pageAs(pageQuery.buildPage(), query, AttactmentVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增数据
     *
     * @param attactmentBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insertByBo(AttactmentBo attactmentBo) {
        Attactment attactment = MapstructUtils.convert(attactmentBo, Attactment.class);
        return save(attactment);
    }

    /**
     * 修改数据
     *
     * @param attactmentBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean updateByBo(AttactmentBo attactmentBo) {
        Attactment attactment = MapstructUtils.convert(attactmentBo, Attactment.class);
        return updateById(attactment);
    }

    /**
     * 通过主键删除数据
     *
     * @param attactIds 主键
     * @return 是否成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> attactIds) {
        return removeByIds(attactIds);
    }

    /**
     * 校验附件业务名称是否唯一
     *
     * @param attactmentBo 附件文件信息
     * @return 校验结果
     */
    @Override
    public Boolean checkAttactmentApplyNameUnique(AttactmentBo attactmentBo) {
        return exists(query()
            .and(bracket(
                AttactmentTableDef.ATTACTMENT.APPLY_NAME.eq(attactmentBo.getApplyName()))
                .or(AttactmentTableDef.ATTACTMENT.APPLY_CODE.eq(attactmentBo.getApplyCode()))
            )
            .ne(Attactment::getAttactId, attactmentBo.getAttactId(), ObjectUtil.isNotNull(attactmentBo.getAttactId())));
    }
}
